<?php	
namespace app\pay\controller\yop;

use think\Controller;
use pay\yop\Config;

use pay\yop\lib\YopRequest;
use pay\yop\lib\YopClient3;
use customtool\Utils;

class OrderQuery extends Controller {
    private $cfg = null;
    
    public function _initialize() {
        $this->cfg = new Config();
    }
    
    /*
     * @params
     * $data    -> post参数
     * $resPayBis     -> model('PayBis')相关参数
     */
    public function index($data, $resPayBis, $resPayBisOrder, $resPayPlatform) {
        $verSign = $data['signature'];
        unset($data['signature']);
        $md5key = $resPayBis->md5key;
        $data['signature'] = payChangyuanSign($data, $md5key); // 创建签名
        
        // 验证签名
        if($verSign == $data['signature']) {
            // 对数据进行解析，向平台发起请求等
            $res = $this->reqYopData($resPayBisOrder, $resPayBis, $resPayPlatform);
            return $res;
        } else {
            // <畅远>签名验证失败
            return changyuanSignFalse();
        } 
    } 
    
    /*
     * 对数据进行解析，向平台发起请求
     */
    private function reqYopData($resPayBisOrder, $resPayBis, $resPayPlatform) {
        $request = new YopRequest("OPR:" . $resPayPlatform->parent_merchant_id, $resPayPlatform->private_key, $this->cfg->C('yopUrl'), $resPayPlatform->public_key);
        $request->addParam("parentMerchantNo", $resPayPlatform->group_merchant_id);
        $request->addParam("merchantNo",       $resPayPlatform->parent_merchant_id);
        $request->addParam("orderId",          $resPayBisOrder->mchnt_orderid);
        $request->addParam("uniqueOrderNo",    $resPayBisOrder->pay_orderid);
        $response = YopClient3::post($this->cfg->C('orderQueryUrl'), $request);

        if($response->validSign==1) { // 平台内部签名验证成功
            // 取得<易宝>返回结果
            $resData = Utils::objectToArray($response);
            
            // 根据返回结果更新 model('PayBisOrder') 订单表
            if( $resData['result'] == NULL) { // 如果返回error，将信息记录到model('PayBisOrder')表中
                $newData['query_status'] = $resData['error'];
                model('PayBisOrder')->updateById($newData, $resPayBisOrder->id);
                return systemFalse(); // 系统异常，请联系畅远技术支持0753-3312399
            } else {
                // 更新订单状态
                orderStatus($resData['result']['status'], $resPayBisOrder->id);
                // 对返回结果进行封装
                $result = $this->packageData($resPayBisOrder, $resPayBis->md5key);
                return $result;  
            }
        } else { // 平台内部签名验证失败
            return paySignFalse();
        } 
    }
 
    /*
     * 对<易宝>返回结果进行封装
     */
    private function packageData($resPayBisOrder, $md5key) {
        $result = array(
            'merchant_id'     => $resPayBisOrder->merchant_id,       // model('PayBis')中的商户号
            'mchnt_orderid'   => $resPayBisOrder->mchnt_orderid,      // 商户订单号
            'pay_orderid'     => $resPayBisOrder->pay_orderid,        // 平台订单号
            'pay_amount'      => $resPayBisOrder->pay_amount,         // 交易金额
            'actual_amount'   => $resPayBisOrder->actual_amount,      // 实际到帐金额
            'status'          => $resPayBisOrder->status,             // 订单状态码, 状态识别model('PayStatus')
            'is_notice'       => $resPayBisOrder->is_notice,          // 付款后通知状态
        );

        return orderQueryTrue($result, $md5key);
    }
}

